<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
// 导出注册方法
import { userRegisterService, userLoginService } from '@/api/user'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
const isRegister = ref(false)
// 表单预校验
const form = ref()
// 表单对象
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})

// 表单规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      // 自定义校验规则 value是传入的值 formModel.value.password是上一个输入框的密码值 callback()是校验函数 成功callback() 失败callback（new Error(message)）
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          // 回调函数callback
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 表单绑定预校验规则
const register = async () => {
  await form.value.validate()
  await userRegisterService(formModel.value)
  ElMessage.success('注册成功')
  // 切换到登录
  isRegister.value = false
}
// 调用存token的userStore
const userStore = useUserStore()
// 调用路由对象实现首页跳转
const router = useRouter()
//userStore.setToken='123'
// 表单登录预校验规则
const login = async () => {
  await form.value.validate()
  // alert('预校验成功')成功后才会打印出来
  const res = await userLoginService(formModel.value)
  userStore.setToken(res.data.token)
  ElMessage.success('登陆成功')
  // 跳转首页
  router.push('/')
}
// 切换的时候重置表单内容适用watch进行监听,需要在vue中导入
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
  <!-- 
    1.结构相关
    在表单组件中，el-row将一行分成24份
  el-col是一列
  :span="12"代表在一行中占12份
  :span="6"代表在一行中占6份
  offset="3"代表在一行中左侧margin的分数
  el-form表示一个标签
  el-form-item表示表单的一行
  el-input表示一个输入框
  2.数据相关
  通过一个数据对象
  1.el-form=>:model="ruleFrom"
  2.el-input=> :roles="rules",绑定整个rules规则3.对象
  4.表单元素 =>v-model="ruleFrom.xxx"
 el-from-item=> prop与ruleFrom要相对应对应的规则

 -->
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册页面 -->
      <!-- ref="form绑定的预校验规则" -->
      <el-form
        ref="form"
        size="large"
        :model="formModel"
        :rules="rules"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <!-- 输入用户名 -->
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="formModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登录页面 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 测试代码 -->
        <!-- <el-form-item>
          <el-input
            :prefix-icon="Compass"
            placeholder="这是一个磁盘测试input"
          ></el-input>
        </el-form-item> -->
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
          >
            登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  //background-color: #fff;
  background: -webkit-linear-gradient(left, #030b18, #11315a, #030b18);
  .bg {
   background:
      // url('@/views/login/images/face.png') no-repeat 60% center / 240px auto,
     url('@/assets/gloal.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
